package com.omenkk.juc.AQS;

import com.omenkk.juc.util.Slepper;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.CountDownLatch;
@Slf4j
public class CountdownLatchDemo {
    public static void main(String[] args) throws InterruptedException {
        CountDownLatch latch=new CountDownLatch(2);
        new Thread(()->{
            try{

                log.debug("运行中1");
                Slepper.sleep(3);
                latch.countDown();//让计数减1
            }finally {
                latch.getCount();
                log.debug("运行完毕1");
            }

        }).start();
        new Thread(()->{
            try{

                log.debug("运行中2");
                Slepper.sleep(3);
                latch.countDown();
            }finally {
                latch.getCount();
                log.debug("运行完毕2");
            }

        }).start();
        log.debug("进入同步");
        latch.await();//等待计数归零，才能继续执行
        log.debug("同步完成");
    }
}
